package com.hzlx.productionProducts.controller;

import java.util.List;
import java.io.IOException;

import com.hzlx.MesBadGoodsDto;
import com.hzlx.ProductionProductsDto;
import com.hzlx.ResultDto;
import com.hzlx.productionProducts.domain.ProductionProductsWithCounts;
import com.hzlx.productionProducts.domain.insertProductsWithCount;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.productionProducts.domain.ProductionProducts;
import com.hzlx.productionProducts.service.IProductionProductsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产产品Controller
 * 
 * @author dk
 * @date 2025-07-01
 */

@RestController
@RequestMapping("/production")
public class ProductionProductsController extends BaseController
{
    @Autowired
    private IProductionProductsService productionProductsService;






    @GetMapping(value = "/productionProductsByOne/{Id}")
    public ResultDto<ProductionProducts> getOneByOperationId(@PathVariable("Id") Integer operationId)
    {
        System.out.println("查询的目标id为"+operationId);
        return ResultDto.<ProductionProducts>builder().data(productionProductsService.selectFirstProductByOperationId(operationId)).build();
    }
    /**
     * 查询生产产品列表
     */
    @RequiresPermissions("production-service:productionProducts:list")
    @GetMapping("/productionProducts/list")
    public TableDataInfo list(ProductionProducts productionProducts)
    {
        startPage();
        List<ProductionProducts> list = productionProductsService.selectProductionProductsList(productionProducts);
        return getDataTable(list);
    }

    /**
     * 导出生产产品列表
     */
    @RequiresPermissions("production-service:productionProducts:export")
    @Log(title = "生产产品", businessType = BusinessType.EXPORT)
    @PostMapping("/productionProducts/export")
    public void export(HttpServletResponse response, ProductionProducts productionProducts)
    {
        List<ProductionProducts> list = productionProductsService.selectProductionProductsList(productionProducts);
        ExcelUtil<ProductionProducts> util = new ExcelUtil<ProductionProducts>(ProductionProducts.class);
        util.exportExcel(response, list, "生产产品数据");
    }

    /**
     * 获取生产产品详细信息
     */
    @RequiresPermissions("production-service:productionProducts:query")
    @GetMapping(value = "/productionProducts/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Integer productId)
    {
        return success(productionProductsService.selectProductionProductsByProductId(productId));
    }

    /**
     * 新增生产产品
     */
    @RequiresPermissions("production-service:productionProducts:add")
    @Log(title = "生产产品", businessType = BusinessType.INSERT)
    @PostMapping("/productionProducts")
    public AjaxResult add(@RequestBody ProductionProducts productionProducts)
    {
        return toAjax(productionProductsService.insertProductionProducts(productionProducts));
    }

    /**
     * 修改生产产品
     */
    @RequiresPermissions("production-service:productionProducts:edit")
    @Log(title = "生产产品", businessType = BusinessType.UPDATE)
    @PutMapping("/productionProducts")
    public AjaxResult edit(@RequestBody ProductionProducts productionProducts)
    {
        return toAjax(productionProductsService.updateProductionProducts(productionProducts));
    }

    /**
     * 删除生产产品
     */
    @RequiresPermissions("production-service:productionProducts:remove")
    @Log(title = "生产产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/productionProducts/{productIds}")
    public AjaxResult remove(@PathVariable Integer[] productIds)
    {
        return toAjax(productionProductsService.deleteProductionProductsByProductIds(productIds));
    }


    @PutMapping("/productionProductsByFinished")
    public ResultDto<String> editByFinished(@RequestBody ProductionProductsDto productionProductsDto)
    {
         productionProductsService.updateProductionProductsByFinished(productionProductsDto);
         return ResultDto.<String>builder().code(200).build();
    }


    @PostMapping("/productionProductsBydo")
    public ResultDto<String> removeByDo(@RequestBody MesBadGoodsDto mesBadGoodsDto)
    {

       productionProductsService.deleteProductionProductsByDo(mesBadGoodsDto.getProductId());
        System.out.println("成功调用"+"REMOVEBYdo");
       return ResultDto.<String>builder().code(200).build();
    }

    @PostMapping("/add/badGoods")
    public ResultDto<String> addBadGoods(@RequestBody ProductionProductsDto productionProductsDto){
        productionProductsService.insertProductionProductsByBadGoods(productionProductsDto);
        return ResultDto.<String>builder().code(200).build();
    }
    @PostMapping("/update/ProductsWithCount")
    public ResultDto<String> updateByCount(@RequestBody ProductionProductsWithCounts productionProductsWithCounts){
      return productionProductsService.updateProductionProductsByCounts(productionProductsWithCounts);
    }
    @PostMapping("/insert/productsWithCount")
    public ResultDto<String> insertWithCount(@RequestBody insertProductsWithCount insertProductsWithCount){
        productionProductsService.insertProductsWithCount(insertProductsWithCount);
        return ResultDto.<String>builder().code(200).build();
    }
}
